package com.fanshuai.mybatis_plus.repository.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.fanshuai.mybatis_plus.dao.WechatInfoDao;
import com.fanshuai.mybatis_plus.domain.WechatInfo;
import com.fanshuai.mybatis_plus.entity.WechatInfoEntity;
import com.fanshuai.mybatis_plus.repository.WechatInfoRepository;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Repository;

import javax.annotation.Resource;

@Repository
public class WechatInfoRepositoryImpl implements WechatInfoRepository {
    @Resource
    private WechatInfoDao wechatInfoDao;

    public WechatInfoEntity toEntity(WechatInfo info) {
        WechatInfoEntity entity = new WechatInfoEntity();
        BeanUtils.copyProperties(info, entity);

        return entity;
    }

    public WechatInfo toDomain(WechatInfoEntity entity) {
        WechatInfo info = new WechatInfo();
        BeanUtils.copyProperties(entity, info);
        return info;
    }

    @Override
    public void insert(WechatInfo wechatInfo) {
        wechatInfoDao.save(toEntity(wechatInfo));
    }

    @Override
    public void update(WechatInfo wechatInfo) {
        wechatInfoDao.updateById(toEntity(wechatInfo));
    }

    @Override
    public void delete(long uid) {
        QueryWrapper<WechatInfoEntity> wrapper = new QueryWrapper<>();
        wrapper.eq("user_id", uid);
        wechatInfoDao.remove(wrapper);
    }

    @Override
    public WechatInfo query(long uid) {
        QueryWrapper<WechatInfoEntity> wrapper = new QueryWrapper<>();
        wrapper.eq("user_id", uid);
        WechatInfoEntity entity = wechatInfoDao.getOne(wrapper);

        return toDomain(entity);
    }
}
